<template>
  <div class="forget">
    <div class="leftView">
      <div class="textAlignCenter marginBottom40">
        <img src="@/assets/logo/logo-title.png" alt="" width="215" height="42">
      </div>
      <h3 class="title">{{ titleName }}</h3>
      <el-form ref="form"
               :model="form"
               :rules="rules"
               :label-position="labelPosition"
               class="forget-form">
<!--        第一步-->
        <template v-if="stepName==='one'">
          <el-form-item prop="account" label="账号">
            <el-input v-model="form.account" type="text" auto-complete="off" placeholder="请输入账号" />
          </el-form-item>
          <el-form-item prop="code" label="验证码">
            <el-input
              v-model="form.code"
              auto-complete="off"
              placeholder="请输入计算后的数字"
              style="width: 63%"
              @keyup.enter.native="handleSendCode"
            />
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleSendCode"
            >
              <span v-if="!loadingOne">获取短信验证码</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </template>
<!--        第二步-->
        <template v-if="stepName==='two'">
          <el-form-item prop="code" label="短信验证码">
            <el-input
              v-model="form.verifyCode"
              auto-complete="off"
              placeholder="请输入收到的短信验证码"
              style="width: 186px"
              @keyup.enter.native="handleSubmit"
            />
            <el-button class="resendBtn" type="primary" :disabled="resendDisable" @click="handleResend($event)">重新发送</el-button>
          </el-form-item>

          <el-form-item prop="resetPwd" label="新密码">
            <el-input v-model="form.resetPwd" type="password" auto-complete="off" placeholder="请输入新密码" />
          </el-form-item>
          <el-form-item prop="secPassword" label="确认密码">
            <el-input v-model="form.secPassword" type="password" auto-complete="off" placeholder="请再次输入新密码" @keyup.enter.native="handleSubmit"
            />
          </el-form-item>

          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleSubmit"
            >
              <span v-if="!loading">重置密码</span>
              <span v-else>重 置 密 码 中...</span>
            </el-button>
          </el-form-item>
        </template>
<!--        第三步-->
        <template v-if="stepName==='three'">
          <img src="@/assets/image/success.png" alt="" width="110" height="100" class="displayBlock marginTB40 marginAuto">
          <h4 class="textAlignCenter marginBottom5">账号密码重置成功</h4>
          <h4 class="textAlignCenter marginBottom40">请返回登录页面重新登录</h4>
          <el-button class="widthMax" type="primary" @click="handleToLogin">登  录</el-button>
        </template>

      </el-form>

      <div v-if="stepName !== 'three'" class="width300 textAlignRight marginAuto">
        <router-link :to="{ path: '/login' }"><span class="fontSize14 colorTheme">密码想起来？点击登录</span></router-link>
      </div>
      <div class="textAlignCenter">
        <div class="stepView">
          <el-steps direction="vertical" :active="activeStep">
            <el-step title="获取短信验证码"></el-step>
            <el-step title="重置登录密码"></el-step>
            <el-step title="重置密码成功"></el-step>
          </el-steps>
        </div>
        <span class="fontSize12 color999">如更换手机号请联系管理员</span>
      </div>
    </div>


  </div>
</template>

<script>
import { getCodeImg, getMobileCode, getResetPwd } from "@/api/forget";

export default {
  name: "ForgetPassword",
  data() {
    let validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'));
      } else if (value !== this.form.resetPwd) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      titleName: '忘记密码',
      stepName: 'one', // 当前 第几步骤
      activeStep: 1,  // 当前完成几步
      resendDisable: false,
      codeUrl: "",
      labelPosition: "top",
      imgUUid: "",
      form: {
        account: "", // 账号
        code: "",  // 图片验证码
        verifyCode: "",
        resetPwd: "", // 新密码
        secPassword: "", // 确认密码
        uuid: ""
      },
      rules: {
        account: [{ required: true, trigger: "blur", message: "账号不能为空" }],
        code: [{ required: true, trigger: "change", message: "验证码不能为空" }],
        verifyCode: [{ required: true, trigger: "change", message: "短信验证码不能为空" }],
        resetPwd: [{ required: true, trigger: "blur", message: "新密码不能为空" }],
        secPassword: { validator: validatePass2, trigger: 'blur' },
      },
      loadingOne: false,
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.imgUUid = res.uuid;
      });
    },

    handleSendCode() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loadingOne = true;
          this.form.uuid = this.imgUUid;
          // 获取短信验证码 接口
          getMobileCode(this.form).then(res => {
            this.form.uuid = res.msg;
            this.stepName = 'two';
            this.activeStep = 2;
            this.titleName = '重置登录密码';
          }).catch((err) => {
            this.loadingOne = false;
            this.getCode();
            console.log(err)
          });
        }
      });
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true;
          getResetPwd(this.form).then( res => {
            this.loading = false;
            // 请求 重置密码 验证接口
            this.stepName = 'three';  // 第三步
            this.activeStep = 3;
            this.titleName = '重置成功';
          }).catch((err) => {
            this.loading = false;
            console.log(err)
          });
        }
      });
    },
    handleResend(e) {
      this.resendDisable = true;
    //  this.handleSendCode();
      this.form.uuid = this.imgUUid;
      // 获取短信验证码 接口
      getMobileCode(this.form).then(res => {
        this.form.uuid = res.msg;
        this.stepName = 'two';
        this.activeStep = 2;
        this.titleName = '重置登录密码';
      }).catch((err) => {
        this.loadingOne = false;
        this.getCode();
        console.log(err)
      });
      let time = 10;
      let timer = setInterval(function(){
        time--;
        e.target.innerText = "（"+time+"秒）重发";
        if(time === 0){
          clearInterval(timer);
          e.target.innerText = "重新发送";
        }
      },1000);
      if(e.target.innerText === "重新发送"){
        this.resendDisable = false;
      }

    },
    handleToLogin() {
      this.$router.push({ path: '/login'})
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.forget {
  height: 100vh;
  background-image: url("../assets/image/bg.png");
  background-size: cover;
}
.leftView{
  padding-top: 60px;
  width: 500px;
  height: 100%;
  background: #fff;
}
.title {
  margin: 0 auto 10px auto;
  font-size: 20px;
  text-align: center;
  color: #4667db;
  font-weight: 400;
}

.forget-form {
  width: 300px;
  margin: 0 auto;
  border-radius: 6px;
  background: #ffffff;

  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.resendBtn{
  width: 100px;
  height: 36px;
  float: right;
  padding: 10px 0;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login-code-img {
  height: 38px;
}
.el-login-footer {
  position: fixed;
  bottom: 100px;
  width: 400px;
  text-align: center;
  color: #555;
  font-size: 12px;
}
/*步骤条*/
  .stepView{
    margin: 30px auto;
    width: 150px;
    height: 150px;
    ::v-deep .el-step__title.is-finish,
    ::v-deep .el-step__title.is-process,
    ::v-deep .el-step__title.is-wait{
      color: #555;
      font-weight: 400;
    }
    ::v-deep .el-step.is-vertical .el-step__line{
      border-color: transparent;
      top: 1px;
      left: 10px;
      border-style: dashed;
      background: transparent;
      &:after {
        content: '•••';
        position: absolute;
        top: 40px;
        left: 50%;
        color: #C0C4CC;
        transform: translate(-50%, -50%) rotate(90deg);
        font-size: 20px;
        letter-spacing: 3px;
      }
    }
    ::v-deep .el-step__head.is-finish{
      color: #fff;
      .el-step__icon.is-text{
        background: #4667db;
        border-width: 1px;
      }
      .el-step__line{
        &:after {
          color: #4667db;
        }
      }
    }
    ::v-deep .el-step__head.is-process{
      color: #555;
      .el-step__icon.is-text{
        background: #fff;
        border-color: #C0C4CC;
        border-width: 1px;
      }
      .el-step__line{
        &:after {
          color: #C0C4CC;
        }
      }
    }
    ::v-deep .el-step__head.is-wait{
      color: #555;
      .el-step__icon.is-text{
        background: #fff;
        border-width: 1px;
      }
    }

    ::v-deep .el-step__icon-inner{
      font-weight: 400;
    }
  }
  .icon-verified{
    display: block;
    margin: 40px auto;
    text-align: center;
    font-size: 80px;
    color: #67c23a;
  }
::v-deep .el-form-item.is-required:not(.is-no-asterisk)>.el-form-item__label:before {
  content: '';  // 去掉 * 号
  color: #ff4949;
  margin-right: 4px;
}
::v-deep .el-input--medium .el-input__inner{
  height: 36px;
}
::v-deep .el-button--primary {
  color: #fff;
  background-color: #4164E3;
  border-color: #4164E3;
}
</style>
